package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.constants.SystemConstants;
import com.haoshui.domain.entity.Menu;
import com.haoshui.domain.vo.MenuTreeVo;
import com.haoshui.domain.vo.MenuVo;
import com.haoshui.mapper.MenuMapper;
import com.haoshui.service.MenuService;
import com.haoshui.service.UserRoleService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 20:35:24
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public List<String> selectPermsByUserId(Long userId) {
        //如果用户id是1，返回所有权限
        if(userId == 1L){
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Menu::getMenuType, SystemConstants.MENU, SystemConstants.BUTTON);
            queryWrapper.eq(Menu::getStatus, SystemConstants.MENU_NORMAL);
            List<Menu> menuList = list(queryWrapper);

            List<String> perms = menuList.stream()
                    .map(Menu::getPerms)
                    .collect(Collectors.toList());
            
            return perms;
        }
        
        //查询其他用户的perms
        return getBaseMapper().selectPermsByUerId(userId);
    }

    @Override
    public List<MenuVo> selectMenuTreeByUserId(Long userId) {
        List<Menu> menus = null;
        //如果用户是超级用户，返回全部的组件
        //查找用户role
        List<Long> roleIdsByUserId = userRoleService.getRoleIdsByUserId(userId);
        boolean loop = roleIdsByUserId.contains(1L);
        if(loop){
            menus =  menuMapper.selectMenuForSuperUser(userId);
        }else{
            menus = menuMapper.selectMenuByUserId(userId);
        }

        //封装成Vo
        List<MenuVo> menuVos = BeanCopyUitls.copyBeanList(menus, MenuVo.class);

        //获取子菜单
        List<MenuVo> collect = menuVos.stream()
                .filter(menuVo -> menuVo.getParentId().equals(0L))
                .map(menuVo -> menuVo.setChildren(getChildren(menuVo, menuVos)))
                .collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<Menu> listMenuByCondition(String status, String menuName) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(status) && StringUtils.hasText(status), Menu::getStatus, status);
        queryWrapper.like(Objects.nonNull(menuName) && StringUtils.hasText(menuName), Menu::getMenuName, menuName);

        queryWrapper.orderByAsc(Menu::getParentId, Menu::getOrderNum);
        List<Menu> list = list(queryWrapper);
        return list;
    }

    @Override
    public boolean addMenu(Menu menu) {
        boolean save = save(menu);
        return save;
    }

    @Override
    public Menu findMenuByMenuId(Long id) {
        Menu byId = getById(id);

        return byId;
    }

    @Override
    public boolean hasChildren(Long id) {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId, id);
        queryWrapper.eq(Menu::getDelFlag, SystemConstants.MENU_NORMAL);
        List<Menu> list = list(queryWrapper);
        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<MenuTreeVo> getMenuTree() {
        //查询出所有的菜单
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Menu::getOrderNum);
        List<Menu> list = list();

        //每一个都封装成vo
        List<MenuTreeVo> collect = list.stream()
                .map(menu -> new MenuTreeVo(null, menu.getId(), menu.getMenuName(), menu.getParentId()))
                .collect(Collectors.toList());

        //构建孩子
        List<MenuTreeVo> menuTreeVos = collect.stream()
                .filter(vo -> vo.getParentId().equals(0L))
                .map(vo -> vo.setChildren(getChildren(vo, collect)))
                .collect(Collectors.toList());

        return menuTreeVos;
    }

    private List<MenuVo> getChildren(MenuVo menuVo, List<MenuVo> menuVos) {
        List<MenuVo> collect = menuVos.stream()
                .filter(menuVo1 -> menuVo1.getParentId().equals(menuVo.getId()))
                .map(menuVo1 -> menuVo1.setChildren(getChildren(menuVo1, menuVos)))
                .collect(Collectors.toList());
        return collect;
    }

    private List<MenuTreeVo> getChildren(MenuTreeVo menuVo, List<MenuTreeVo> menuVos) {
        List<MenuTreeVo> collect = menuVos.stream()
                .filter(menuVo1 -> menuVo1.getParentId().equals(menuVo.getId()))
                .map(menuVo1->menuVo1.setChildren(getChildren(menuVo1, menuVos)))
                .collect(Collectors.toList());
        return collect;
    }
}
